#!/bin/bash

#读配置
while read LINE;
do
    if [ "$(echo $LINE | grep "root_path: ")" != "" ];then
        root_path=$(echo "$LINE" | awk -F': ' '{print $2}')
	break
    else
        continue
    fi
done < varinfo.yml

#清理缓存
upload_path=$root_path/upload
cache_path=$upload_path/.cache
mkdir -p $cache_path
rm -rf $cache_path/*

#单独更新微服务
function update_service {
    #初始化微服务变量
    sncpackage=$cache_path/sncpackage
    echo "allwfw:" > $sncpackage
 
    # 查找以特定前缀开头的文件
    snc_files=($(ls ${upload_path}/snc-* 2>/dev/null))
    atm_files=($(ls ${upload_path}/atm_* 2>/dev/null))
    web_files=($(ls ${upload_path}/web_* 2>/dev/null))

    # 更新标志位
    if [[ ${#snc_files[@]} -gt 0 ]]; then
      has_snc=1
    fi
    if [[ ${#atm_files[@]} -gt 0 ]]; then
      has_atm=1
    fi
    if [[ ${#web_files[@]} -gt 0 ]]; then
      has_web=1
    fi
    
    # 检查共存情况
    if [[ $has_snc -eq 1 && ($has_atm -eq 1 || $has_web -eq 1) ]]; then
      echo "错误：snc- 和 atm_ 或 web_ 文件共存！"
      exit 1
    elif [[ $has_atm -eq 1 && $has_web -eq 1 ]]; then
      echo "atm_ 和 web_ 文件共存，没有问题。"
    else
      echo "文件检查通过，没有冲突。"
    fi

    #解压文件
    cd ${upload_path}
    for i in `ls ${upload_path}`
    do
        case "$i" in
            *.zip)
                unzip $i -d ./
                ;;
            *.tar.gz|*.tgz|*.tar)
                tar -xvf $i -C ./
                ;;
            *)
                echo "没有压缩包"
        esac
    done
    
    #删除上传的源文件
    find ${upload_path} -maxdepth 1 -type f -name "*.zip" -o -name "*.tgz" -o -name "*.tar.gz" -o -name "*.tar"| xargs rm -rf
    #删除MacOS打包文件    
    find ${upload_path} -maxdepth 1 -type d -name "*MACOSX*" | xargs rm -rf

    # 统一压缩为tar
    cd ${upload_path}
    for i in $(ls ${upload_path})
    do
        tar -zcvf $i.tar.gz $i
        rm -rf $i
        # 解压记录,生成变量文件
        echo "  - $i" >> $sncpackage
    done
    # 查看生成的清单文件    
    cat $sncpackage
    houtai=`cat $sncpackage | grep -A 1 "allwfw:" | grep "snc-" | wc -l`
    qiantai=`cat $sncpackage | grep -A 1 "allwfw:" | grep -E "web_|atm_" | wc -l`

    # 调用ansible执行更新
    #静态iventory方式
    #ansible-playbook -i ${script_home}/ansible/hosts -e "@$sncpackage" $root_path/update_svc.yaml
    
    #动态iventory方式
    chmod +x $root_path/inventory.py
    #后端更新
    if [ $houtai -gt 0 ] && [ $qiantai -eq 0 ]; then 
        echo "更新后端"
        ansible-playbook -i $root_path/inventory.py -e "@$sncpackage" $root_path/change_service_backend.yml
    #前端更新
    elif [ $houtai -eq 0 ] && [ $qiantai -gt 0 ]; then
        echo "更新前端"
        ansible-playbook -i $root_path/inventory.py -e "@$sncpackage" $root_path/change_service_front.yml
    else
        echo "有脏数据，请检查"
        exit 1
    fi
    
    # 清空上传目录
    #rm -rf ${upload_path}/*

    # 迁移上传目录
    currtime=`echo $(date +%Y%m%d%H%M%S)`
    mv ${upload_path} $root_path/history$currtime && mkdir ${upload_path}
    
    # 删除超过10个历史文件
    find $root_path/ -type d -name "history*" | sort | head -n -10 | xargs rm -rf 
}

update_service
